#include <QDebug>
#include <QStackedWidget>
#include <QVBoxLayout>
#include <QTableView>
#include <QMainWindow>
#include "BottomDockWidget.h"
#include "Console.h"
#include "SearchResult.h"
#include "ErrorTable.h"

BottomDockWidget::BottomDockWidget(const QString &title, QWidget *parent, Qt::WindowFlags flags):
    QDockWidget(title, parent, flags)
{
    init();
}

BottomDockWidget::BottomDockWidget(QWidget *parent, Qt::WindowFlags flags) :
    QDockWidget(parent, flags)
{
    init();
}

void BottomDockWidget::showEvent(QShowEvent *)
{
    _stack = new QStackedWidget(widget());
    _stack->addWidget(_console);
    _stack->addWidget(new SearchResult(this));
    ErrorTable * table = new ErrorTable(this);
    connect(table, SIGNAL(activated(int)), SIGNAL(jimpCurrentFileLine(int)));
    _stack->addWidget(table);
    setWidget(_stack);
    widget()->hide();
}

void BottomDockWidget::init()
{
    QWidget *title = titleBarWidget();
    DummyTitle *title2 = new DummyTitle(this);
    setTitleBarWidget(title2);
    delete title;
    _console = new Console(this);
}

void BottomDockWidget::modeSelected(DockShowMode mode)
{
    if (mode == Show){
        widget()->show();
    }
    if (mode == Hide){
        widget()->hide();
    }
    if (mode == ShowRun){
        emit dockModeSelected(ShowRun);
        _stack->setCurrentIndex(0);
        widget()->show();
    }
    if (mode == ShowSearch){
        emit dockModeSelected(ShowSearch);
        _stack->setCurrentIndex(1);
        widget()->show();
    }
    if (mode == ShowErrors){
        emit dockModeSelected(ShowErrors);
        _stack->setCurrentIndex(2);
        widget()->show();
    }
}

void BottomDockWidget::errorListChanged(QAbstractTableModel* model)
{
    qobject_cast<QTableView*>(_stack->widget(2))->setModel(model);
}

Plugins::IConsole * BottomDockWidget::console()
{
    return _console;
}

QWidget * BottomDockWidget::consoleWindow()
{
    return _console;
}

QSize DummyTitle::minimumSizeHint() const
{
    return QSize(1, 1);
}

QSize DummyTitle::sizeHint() const
{
    return QSize(1, 1);

}

